package com.sz.app.oss.api.system;

import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.common.base.dto.I18nDto;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.service.I18nBaseService;
import com.sz.common.base.utils.EntityUtils;
import com.sz.common.core.service.PrincipalUtils;
import com.sz.common.core.system.dto.MenuItem;
import com.sz.common.core.system.entity.Menu;
import com.sz.common.core.system.service.MenuService;
import com.sz.app.oss.api.system.dto.MenuDto;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Function: 菜单controller <br>
 * Author: hpeng.wang <br>
 * Date: 2016-10-27 11:09:00
 */
@RestController
@RequestMapping("${api}/sys/menus")
@Api(description = " ", tags = "005、菜单资源")
public class MenuController extends BaseController {
    @Autowired
    private MenuService service;
    @Autowired
    private I18nBaseService i18nService;
    @Value("${i18n.sys.menu}")
    private String i18nTable;

    /**
     * 保存
     *
     * @param dto 要保存的实体对象
     * @return 操作结果
     */
    @ApiOperation(value = "新增", notes = "给某个域下增加新的菜单<br>权限：" + Permissions.SYS_MGT_MENU)

    @RequiresPermissions(Permissions.SYS_MGT_MENU)
    @RequestMapping(value = "", method = {RequestMethod.POST})
    public ResultDto save(@RequestBody MenuDto dto) {
        Menu entry = new Menu();
        dto.setId(null);//保存时忽略id
        EntityUtils.copyPropertiesIgnoreNull(dto, entry);
        int id = service.save(entry);

        ResultDto result = new ResultDto();
        result.setData(id);
        return result;


    }

    /**
     * 删除
     *
     * @param id
     * @return 操作结果
     */
    @ApiOperation(value = "删除", notes = "根据指定菜单id删除指定的菜单<br>" + Permissions.SYS_MGT_MENU, httpMethod = "DELETE")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单Id", required = true, dataType = "int", paramType = "path")})

    @RequiresPermissions(Permissions.SYS_MGT_MENU)
    @RequestMapping(value = "/{id}", method = {RequestMethod.DELETE})
    public ResultDto delete(@PathVariable int id) {
        service.delete(id);
        ResultDto result = new ResultDto();
        result.setData(id);
        return result;

    }

    /**
     * 修改菜单.
     *
     * @param id      要修改的菜单id
     * @param menuDto 需要修改的菜单实体
     * @return 操作结果
     */

    @ApiOperation(value = "修改", notes = "对指定的菜单进行修改<br>权限：" + Permissions.SYS_MGT_MENU)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "修改菜单的id ", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "menuDto", value = "提交的菜单信息", required = true, dataType = "MenuDto", paramType =
                    "body")})
    @RequestMapping(value = "{id}", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.SYS_MGT_MENU)
    public ResultDto update(@PathVariable int id, @RequestBody MenuDto menuDto) {

        menuDto.setId(null);
        Menu menu = service.findById(id);
        EntityUtils.copyPropertiesIgnoreNull(menuDto, menu);
        service.update(menu);
        ResultDto dto = new ResultDto();
        dto.setData(id);
        return dto;
    }

    /**
     * 查询指定id的菜单.
     *
     * @param id 菜单id
     * @return 操作结果
     */
    @RequestMapping(value = "{id}", method = {RequestMethod.GET})
    @ApiOperation(value = "查询_id", notes = "根据菜单id获取指定菜单<br>权限：" + Permissions.SYS_MGT_MENU, httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "菜单Id", required = true, dataType = "int", paramType = "path")})
    @RequiresPermissions(Permissions.SYS_MGT_MENU)
    public ResultDto findById(@PathVariable Integer id) {

        Menu ss = service.findById(id);
        ResultDto result = new ResultDto();
        result.setData(ss);
        return result;
    }


    /**
     * 批量新增或修改国际化.
     *
     * @param dtos 要保存或修改的国际化实体集合
     * @return 操作结果
     */
    @ApiOperation(value = "新增或修改i18n", notes = "批量新增或修改费用类型国际化<br>权限：menu:mgt")
    @RequestMapping(value = "i18n", method = {RequestMethod.POST})
    public ResultDto saveI18n(@RequestBody @ApiParam(value = "要保存的对象集合", required = true)
                                      List<I18nDto> dtos) {
        i18nService.save(dtos, i18nTable);
        ResultDto result = this.getSaveI18nSuccessDto();
        return result;
    }


    /**
     * 获取指定域的全部菜单列表.
     *
     * @return 操作结果
     */
    @RequestMapping(value = "/tree", method = {RequestMethod.GET})
    @ApiOperation(value = "获取全部菜单", notes = "获取某个域下的全部菜单树<br>权限：" + Permissions.SYS_MGT_MENU)
    @RequiresPermissions(Permissions.SYS_MGT_MENU)
    public ResultDto getMenuTree() {
        logBefore(logger, "获取全部菜单");
        Integer domainId = PrincipalUtils.getDomainId();
        String localLanguage = PrincipalUtils.getLocalLanguage();
        MenuItem root = service.getMenuTree(domainId, localLanguage, true, true);

        ResultDto result = new ResultDto();
        result.setData(root);
        return result;
    }


    /**
     * 根据角色id获取相应的菜单
     *
     * @param roleId 角色id
     * @return 操作结果
     */
    @RequestMapping(value = "/roletree/{roleId}", method = {RequestMethod.GET})
    @ApiOperation(value = "获取指定角色下的菜单", notes = "获取指定角色下的菜单<br>权限：" + Permissions.SYS_MGT_MENU + "||" + Permissions.SYS_MGT_ROLE)
    @RequiresPermissions(value = {Permissions.SYS_MGT_MENU, Permissions.SYS_MGT_ROLE}, logical = Logical.OR)
    public ResultDto getRoleMenuTree(@ApiParam(value = "roleId", required = true) @PathVariable Integer roleId) {
        MenuItem root = service.getRoleMenuTree(roleId);
        ResultDto result = new ResultDto();
        result.setData(root);
        return result;
    }

    @RequestMapping(value = "/roleandpermissiontree/{roleId}", method = {RequestMethod.GET})
    @ApiOperation(value = "获取指定角色下的菜单以及权限", notes = "获取指定角色下的菜单操作<br>权限：" + Permissions.SYS_MGT_MENU + "||" + Permissions.SYS_MGT_ROLE)
    @RequiresPermissions(value = {Permissions.SYS_MGT_MENU, Permissions.SYS_MGT_ROLE}, logical = Logical.OR)
    public ResultDto getRolePermissionMenuTree(@ApiParam(value = "roleId", required = true) @PathVariable Integer roleId) {
        MenuItem root = service.getRoleMenuAndPermissionTree(roleId);
        ResultDto result = new ResultDto();
        result.setData(root);
        return result;
    }


}
